<?php

namespace App\Http\Middleware;

use Closure;
use App\Repository\Enum\ScopeEnum;
use App\Repository\TokenRepository;
use Dingo\Api\Exception\StoreResourceFailedException;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;

class ExclusiveScopeMiddleware
{
    /**
     * 普通用户的访问权限.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (TokenRepository::getCurrentTokenVar('scope')){
            if ( TokenRepository::getCurrentTokenVar('scope') != ScopeEnum::USER){
                throw new AccessDeniedHttpException('暂无访问权限', null, 10001);
            }
            return $next($request);
        }else{
            throw new StoreResourceFailedException('token无效或者已过期', null, null, [] , 10001);
        }
    }
}
